<?php
require "includes/conf.php";
require "includes/functions.php";

$section = "listpacientes";
$sectiondes = "LISTADO DE PACIENTES";
require "includes/header.inc.php";

$searchstr = (isset($_GET["searchstr"]) && $_GET["searchstr"] != "" ? normaliza($_GET["searchstr"]) : "");

$order = (isset($_GET["order"]) && $_GET["order"] != "" ? $_GET["order"] : "apellidos");
if ($order == "apellidosinv") $order = "apellidos DESC, nombre DESC";
else if ($order == "nombre") $order = "nombre, apellidos";
else if ($order == "nombreinv") $order = "nombre DESC, apellidos DESC";
else $order = "apellidos, nombre";

$querystring = "";
if ($_SERVER["QUERY_STRING"] != "")
{
	$qstr = explode("&", $_SERVER["QUERY_STRING"]);
	foreach ($qstr as $qitem) if (strpos($qitem, "order") === false) $querystring .= $qitem."&";
	$querystring = "&".substr($querystring, 0, strlen($querystring)-1);
}

$query = "SELECT id_paciente FROM pacientes".($searchstr != "" ? " WHERE nombre_n LIKE '%".mysql_real_escape_string($searchstr)."%'" : "");
$qpac1 = mysql_query($query, $sock);
$numrows1 = mysql_num_rows($qpac1);

$numpags = ceil($numrows1 / PAG_LIMIT);

$pag = isset($_GET["pag"]) ? $_GET["pag"] : 1;
if ($pag < 0) $pag = 1;
if ($pag > $numpags) $pag = $numpags;

$anterior = "";
if ($pag > 1)
{
	$_GET["pag"] = $pag-1;
	$get = array();
	foreach ($_GET as $key => $value) $get[] = $key."=".$value;
	$url = $_SERVER["PHP_SELF"]."?".implode("&", $get);
	$anterior = "<a href=\"".$url."\">Anterior</a>";
}

$siguiente = "";
if ($pag < $numpags)
{
	$_GET["pag"] = $pag+1;
	$get = array();
	foreach ($_GET as $key => $value) $get[] = $key."=".$value;
	$url = $_SERVER["PHP_SELF"]."?".implode("&", $get);
	$siguiente = "<a href=\"".$url."\">Siguiente</a>";
}

if ($anterior != "" && $siguiente != "") $nav1 = $anterior." - ".$siguiente." :: ";
else if ($anterior != "" && $siguiente == "") $nav1 = $anterior." :: ";
else if ($anterior == "" && $siguiente != "") $nav1 = $siguiente." :: ";

$start = PAG_LIMIT * ($pag-1);

$query = "SELECT id_paciente, nombre, apellidos FROM pacientes".($searchstr != "" ? " WHERE nombre_n LIKE '%".mysql_real_escape_string($searchstr)."%'" : "")." ORDER BY ".$order." LIMIT ".$start.", ".PAG_LIMIT;
$qpac2 = mysql_query($query, $sock);
$numrows2 = mysql_num_rows($qpac2);

if ($pag == $numpags) $lastreg = $numrows1;
else $lastreg = $start + PAG_LIMIT;

echo "<form name=\"form1\" action=\"\" method=\"get\">\n";
echo "<input type=\"hidden\" name=\"search\" value=\"search\">\n";

echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"formTable\">\n";
echo "<tr valign=\"bottom\">\n";

echo "<td style=\"padding: 0px 0px 5px 8px\">";
echo "Se han encontrado <b>".$numrows1."</b> pacientes";
if ($numrows2 > 0) echo " (mostrando del ".($start+1)." al ".$lastreg.")";
echo "</td>\n";

echo "<td align=\"right\" style=\"padding: 0px 10px 5px 0px\">\n";
echo "<nobr>Buscar: <input type=\"input\" name=\"searchstr\" class=\"textBox\" style=\"width: 120px\"".(isset($_GET["searchstr"]) ? " value=\"".$_GET["searchstr"]."\"" : "")."><a href=\"javascript:document.form1.submit();\" border=\"0\"><img src=\"images/btt_search.gif\" width=\"18\" height=\"17\" border=\"0\" style=\"margin-bottom: -3px !important; margin-bottom /**/: -5px\"></a></nobr><br>\n";
echo "</td>\n";

echo "</tr>\n";
echo "</table>\n";

echo "</form>\n";

echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"listTable\">\n";

if ($numrows2 > 0)
{
	echo "<tr>\n";
	echo "<td width=\"1%\" class=\"listHeaderFirst\"><nobr>Apellidos&nbsp;&nbsp;<a href=\"".$_SERVER["PHP_SELF"]."?order=apellidos".$querystring."\"><img src=\"images/btt_order.gif\" width=\"12\" height=\"10\" border=\"0\" alt=\"Ordenar de menor a mayor\" title=\"Ordenar de menor a mayor\" class=\"orderButton\"></a><a href=\"".$_SERVER["PHP_SELF"]."?order=apellidosinv".$querystring."\"><img src=\"images/btt_orderinv.gif\" width=\"11\" height=\"10\" border=\"0\" alt=\"Ordenar de mayor a menor\" title=\"Ordenar de mayor a menor\" class=\"orderButton\"></a></nobr></td>\n";
	echo "<td class=\"listHeaderLast\"><nobr>Nombre&nbsp;&nbsp;<a href=\"".$_SERVER["PHP_SELF"]."?order=nombre".$querystring."\"><img src=\"images/btt_order.gif\" width=\"12\" height=\"10\" border=\"0\" alt=\"Ordenar de menor a mayor\" title=\"Ordenar de menor a mayor\" class=\"orderButton\"></a><a href=\"".$_SERVER["PHP_SELF"]."?order=nombreinv".$querystring."\"><img src=\"images/btt_orderinv.gif\" width=\"11\" height=\"10\" border=\"0\" alt=\"Ordenar de mayor a menor\" title=\"Ordenar de mayor a menor\" class=\"orderButton\"></a></nobr></td>\n";
	echo "<td width=\"1%\" class=\"listHeaderLast\">&nbsp;</td>\n";
	echo "</tr>\n";

	$contador = 1;

	while ($rpac2 = mysql_fetch_object($qpac2))
	{
		$tdbgcolor = $contador % 2 != 0 ? "#fdfdfd" : "#f4f4f4";
		$tdbgcolor_over = "#e6e6e6";

		echo "<tr onmouseover=\"document.getElementById('lastnameTd".$contador."').style.backgroundColor='".$tdbgcolor_over."'; document.getElementById('nameTd".$contador."').style.backgroundColor='".$tdbgcolor_over."'; document.getElementById('actionsTd".$contador."').style.backgroundColor='".$tdbgcolor_over."';\" onmouseout=\"document.getElementById('lastnameTd".$contador."').style.backgroundColor='".$tdbgcolor."'; document.getElementById('nameTd".$contador."').style.backgroundColor='".$tdbgcolor."'; document.getElementById('actionsTd".$contador."').style.backgroundColor='".$tdbgcolor."';\">\n";
		echo "<td id=\"lastnameTd".$contador."\" class=\"listItemFirst\" style=\"background: ".$tdbgcolor."\"><nobr>".stripslashes($rpac2->apellidos)."</nobr></td>\n";
		echo "<td id=\"nameTd".$contador."\" class=\"listItemLast\" style=\"background: ".$tdbgcolor."\">".stripslashes($rpac2->nombre)."</td>\n";
		echo "<td id=\"actionsTd".$contador."\" class=\"listItemLast\" style=\"background: ".$tdbgcolor."\" align=\"right\"><nobr><a href=\"fichapaciente.php?id=".$rpac2->id_paciente."\">Abrir ficha</a> - <a href=\"informe.php?id=".$rpac2->id_paciente."\">Generar informe</a></nobr></td>\n";
		echo "</tr>\n";

		$contador ++;
	}
}
else
{
	echo "<tr>\n";
	echo "<td width=\"70%\" class=\"listItemFirst\">-</td>\n";
	echo "<td width=\"30%\" class=\"listItemLast\">&nbsp;</td>\n";
	echo "</tr>\n";
}

echo "</table>\n";

if ($numrows2 > 0)
{
	echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"formTable\">\n";
	echo "<tr valign=\"top\">\n";

	echo "<td align=\"right\" style=\"padding: 4px 10px 0px 0px\">\n";
	echo "<nobr>".$nav1."Ir a la p&aacute;gina <select style=\"width: 50px\" onchange=\"gotoPage(this.value, '".$_SERVER["PHP_SELF"]."', '".$_SERVER["QUERY_STRING"]."');\">";
	for ($x=1; $x<=$numpags; $x++) echo "<option value=\"".$x."\"".($x == $pag ? " selected" : "").">".$x."</option>";
	echo "</select></nobr>\n";
	echo "</td>\n";

	echo "</tr>\n";
	echo "</table>\n";
}

echo "<script>document.form1.searchstr.focus();</script>\n";

require "includes/footer.inc.php";
?>
